/*---------------------------------------------------------------------------*
 * Copyright (C) 2014 Ryan M. Murphy <ryan.m.murphy.77@gmail.com>
 *
 * File Name   : usr_chrg.h
 *
 * Project     : Watts-rock
 *               <http://code.google.com/p/watts-rock-motherboard/>
 *
 * Description : This file is responsible for performing all battery charging
 *               operations.
 *
 * Last Update : Jan 4, 2014
 *---------------------------------------------------------------------------*/
#ifndef usr_chrg_h

   #ifdef __cplusplus
   extern "C" {
   #endif

	/*------------------------------------------------------------------------*
	 * Global Defines
	 *------------------------------------------------------------------------*/
	#define usr_chrg_h

	/*------------------------------------------------------------------------*
	 * Include Files
	 *------------------------------------------------------------------------*/
   #include "avr_compiler.h"
   #include "arb_error.h"
   #include "arb_mailbox.h"
   #include "drv_chrg.h"
   #include "usr_bmsFault.h"

	/*------------------------------------------------------------------------*
	 * Global typedefs
	 *------------------------------------------------------------------------*/

	/*------------------------------------------------------------------------*
	 * This enum defines the possible states of the CHRG.
	 *------------------------------------------------------------------------*/
   typedef enum
   {
      /*---------------------------------------------------------------------*
       * The processor has just come out of reset.
       *---------------------------------------------------------------------*/
      CHRG_BOOT = 0,
      /*---------------------------------------------------------------------*
       * The CHRG is in calibration mode.
       *---------------------------------------------------------------------*/
      CHRG_CAL,
      /*---------------------------------------------------------------------*
       * The CHRG has finished calibrating.
       *---------------------------------------------------------------------*/
      CHRG_CAL_FINISHED,
      /*---------------------------------------------------------------------*
       * The charge is waiting to be turned on.
       *---------------------------------------------------------------------*/
      CHRG_IDLE,
      /*---------------------------------------------------------------------*
       * The solar panel can't supply the required constant current or
       * constant voltage, so the perturb and observe algorithm will attempt
       * to track the maximum the power the solar panel can deliver during
       * this time.
       *---------------------------------------------------------------------*/
      CHRG_MPP_CHARGE,
      /*---------------------------------------------------------------------*
       * The battery voltage is within the allowable range for it to be
       * charged at a given maximum current.
       *---------------------------------------------------------------------*/
      CHRG_CC_CHARGE,
      /*---------------------------------------------------------------------*
       * The battery is at the end of its charging cycle, therefore it is being
       * charged with constant voltage until the rate-of-charge drops below
       * a predefined threshold.
       *---------------------------------------------------------------------*/
      CHRG_CV_CHARGE,
      /*---------------------------------------------------------------------*
       * The charge is preparing to power the motherboard down.
       *---------------------------------------------------------------------*/
      CHRG_POWER_DOWN,
      /*---------------------------------------------------------------------*
       * The charge has entered a fault state.
       *---------------------------------------------------------------------*/
      CHRG_FAULT,
      /*---------------------------------------------------------------------*
       * The charge is in manual mode.
       *---------------------------------------------------------------------*/
      CHRG_MAN_CHARGE,
      /*---------------------------------------------------------------------*
       * This variable is always last.
       *---------------------------------------------------------------------*/
      CHRG_NUM_STATES

   }t_chrgState;

	/*------------------------------------------------------------------------*
	 * This enum defines the set of external messages that can be sent back
    * and forth between the CHRG and an external thread.
	 *------------------------------------------------------------------------*/
   typedef enum
   {
      /*---------------------------------------------------------------------*
       * Tells the CHRG to transition into the 'CHRG_POWER_DOWN' state.
       *---------------------------------------------------------------------*/
      CHRG_CMD_PWR_DOWN = 0,
      /*---------------------------------------------------------------------*
       * Tells the CHRG to configure the limits for its particular battery
       * chemistry.
       *---------------------------------------------------------------------*/
      CHRG_CMD_SET_LIMITS,
      /*---------------------------------------------------------------------*
       * Returns the configuration limits for this particular CHRG system
       *---------------------------------------------------------------------*/
      CHRG_CMD_GET_LIMITS,
      /*---------------------------------------------------------------------*
       * Manual charge mode.
       *---------------------------------------------------------------------*/
      CHRG_CMD_MAN_CHRG_CNTRL,
      /*---------------------------------------------------------------------*
       * Requests the CHRG's last known voltage, current, temperature and
       * operational state.
       *---------------------------------------------------------------------*/
      CHRG_CMD_GET_STATE,
      /*---------------------------------------------------------------------*
       * Returns the CHRG's last known voltage, current, temperature and
       * operational state in response to a CHRG_CMD_GET_STATE message.
       *---------------------------------------------------------------------*/
      CHRG_CMD_CURR_STATE,
      /*---------------------------------------------------------------------*
       * Sets the serial number for the CHRG board this particular software
       * is operating on.
       *---------------------------------------------------------------------*/
      CHRG_CMD_SET_BOARD_SERIAL_NUM,
      /*---------------------------------------------------------------------*
       * Turn on/off the fan.
       *---------------------------------------------------------------------*/
      CHRG_CMD_FAN_CNTRL

   }t_chrgMsgId;

	/*------------------------------------------------------------------------*
	 * This structure contains the variables for setting the battery limits
    * for this particular CHRG.
	 *------------------------------------------------------------------------*/
   typedef struct
   {
      /*---------------------------------------------------------------------*
       * The level at which the battery is empty.
       *---------------------------------------------------------------------*/
      float f_lowVoltageThr;
      /*---------------------------------------------------------------------*
       * The level at which the battery is full.
       *---------------------------------------------------------------------*/
      float f_highVoltageThr;
      /*---------------------------------------------------------------------*
       * The level at which the 'pack' output is shut off (in order to prevent
       * damage) due to an external device pulling too much current.
       *---------------------------------------------------------------------*/
      float f_highCurrThr;
      /*---------------------------------------------------------------------*
       * The level at which the 'pack' output is shut off (in order to prevent
       * damage) due to the internal battery chamber temperature getting too
       * hot.
       *---------------------------------------------------------------------*/
      float f_highTempThr;
      /*---------------------------------------------------------------------*
       * The amount of hysteresis on the voltage measurement used for preventing
       * thrashing when transitioning between the various CHRG states.
       *---------------------------------------------------------------------*/
      float f_voltHyst;
      /*---------------------------------------------------------------------*
       * The amount of hysteresis on the temp measurement used for preventing
       * thrashing when transitioning between the various CHRG states.
       *---------------------------------------------------------------------*/
      float f_tempHyst;
      /*---------------------------------------------------------------------*
       * The maximum charge current for the particular battery this CHRG is
       * managing.
       *---------------------------------------------------------------------*/
      float f_maxChargeCurrent;

   }t_chrgLimits;

	/*------------------------------------------------------------------------*
	 * This structure defines the variables for exchanging the status of the
    * CHRG with the outside world.
	 *------------------------------------------------------------------------*/
   typedef struct
   {
	   /*---------------------------------------------------------------------*
	    * The last known voltage, current, and temperature measurement.
	    *---------------------------------------------------------------------*/
      t_chrgMeas t_meas;
	   /*---------------------------------------------------------------------*
	    * The last known CHRG state.
	    *---------------------------------------------------------------------*/
      t_chrgState t_state;
      /*---------------------------------------------------------------------*
       * The percentage of energy left in the battery.
       *---------------------------------------------------------------------*/
      float f_gasGuage;
      /*---------------------------------------------------------------------*
       * The fault status where,
       *              .---.---.---.---.---.---.---.---.
       *    c_fault = | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
       *              '---'---'---'---'---'---'---'---'
       *                            |   |   |   |   |
       *                            |   |   |   |   '-->BMS_FAULT_CURRENT
       *                            |   |   |   '------>BMS_FAULT_TEMP
       *                            |   |   '---------->BMS_FAULT_NO_BATT
       *                            |   '-------------->BMS_FAULT_CHARGE
       *                            '------------------>BMS_FAULT_RAPID_TEMP
       *---------------------------------------------------------------------*/
      uint8_t c_fault;

   }t_chrgMeasStatus;

	/*------------------------------------------------------------------------*
	 * This message contains the variables required for turning on/off the
    * fan.
	 *------------------------------------------------------------------------*/
   typedef struct
   {
	   /*---------------------------------------------------------------------*
	    * The ID for this particular message (CHRG_CMD_FAN_CNTRL)
	    *---------------------------------------------------------------------*/
      uint8_t c_msgId;

	   /*---------------------------------------------------------------------*
	    * If true, the fan is on, otherwise off.
	    *---------------------------------------------------------------------*/
      bool b_enabled;

   }t_chrgFanCntrlMsg;

	/*------------------------------------------------------------------------*
	 * This message contains the variables required for turning on/off the
    * charger output via an external user interface.
	 *------------------------------------------------------------------------*/
   typedef struct
   {
	   /*---------------------------------------------------------------------*
	    * The ID for this particular message (CHRG_CMD_MAN_CHRG_CNTRL)
	    *---------------------------------------------------------------------*/
      uint8_t c_msgId;

	   /*---------------------------------------------------------------------*
	    * The PWM duty cycle for controlling the amount of charge current, where
       * 0.0 < f_pwm < 1.0.
	    *---------------------------------------------------------------------*/
      float f_pwm;

   }t_chrgManChrgMsg;

	/*------------------------------------------------------------------------*
	 * This message contains the variables required for setting the voltage,
    * current, and temperature limits for the CHRG.
	 *------------------------------------------------------------------------*/
   typedef struct
   {
	   /*---------------------------------------------------------------------*
	    * The ID for this particular message (CHRG_CMD_SET_LIMITS)
	    *---------------------------------------------------------------------*/
      uint8_t c_msgId;

	   /*---------------------------------------------------------------------*
	    * The voltage, current, and temperature limits for this particular CHRG.
	    *---------------------------------------------------------------------*/
      t_chrgLimits t_limits;

   }t_chrgSetLimitsMsg;

	/*------------------------------------------------------------------------*
	 * This message sets the CHRG board serial number
	 *------------------------------------------------------------------------*/
   typedef struct
   {
	   /*---------------------------------------------------------------------*
	    * The ID for this particular message (CHRG_CMD_SET_BOARD_SERIAL_NUM)
	    *---------------------------------------------------------------------*/
      uint8_t c_msgId;

	   /*---------------------------------------------------------------------*
	    * The board serial number.
	    *---------------------------------------------------------------------*/
      uint32_t i_boardSerialNum;

   }t_chrgSetBoardSerialNumMsg;

	/*------------------------------------------------------------------------*
	 * This message lets the CHRG know that an external user wants it to
    * power down.
	 *------------------------------------------------------------------------*/
   typedef struct
   {
	   /*---------------------------------------------------------------------*
	    * The ID for this particular message (CHRG_CMD_PWR_DOWN)
	    *---------------------------------------------------------------------*/
      uint8_t c_msgId;

   }t_chrgPwrDownMsg;

	/*------------------------------------------------------------------------*
	 * This message returns the last known CHRG voltage, current, temperature,
    * and operational state in response to a CHRG_CMD_GET_STATE message.
	 *------------------------------------------------------------------------*/
   typedef struct
   {
	   /*---------------------------------------------------------------------*
	    * The ID for this particular message (CHRG_CMD_CURR_STATE)
	    *---------------------------------------------------------------------*/
      uint8_t c_msgId;

      t_chrgMeasStatus t_state;

   }t_chrgCurrStateMsg;

	/*------------------------------------------------------------------------*
	 * Global Variables
	 *------------------------------------------------------------------------*/

	/*------------------------------------------------------------------------*
	 * Global Function Prototypes
	 *------------------------------------------------------------------------*/
   t_error usr_chrgInit( void);

   /*------------------------------------------------------------------------*
    * Returns the last known battery voltage, current, temperature, fuel gage
    * and CHRG state.
    *------------------------------------------------------------------------*/
   t_chrgMeasStatus usr_getBmsMeas( void);

   /*------------------------------------------------------------------------*
    * Returns the last known battery voltage, current, and temperature,
    * limits for the system.
    *------------------------------------------------------------------------*/
   t_chrgLimits *usr_getBmsLimits( void);

   /*------------------------------------------------------------------------*
    * This function returns the mailbox handle for sending messages to the CHRG.
    *------------------------------------------------------------------------*/
   t_MAILBOXHNDL usr_getBmsInMlbxHndle( void);

   /*------------------------------------------------------------------------*
    * This function returns the mailbox handle for receiving messages from the
    * CHRG.
    *------------------------------------------------------------------------*/
   t_MAILBOXHNDL usr_getBmsOutMlbxHndle( void);

   #ifdef __cplusplus
   }/*End extern "C"*/
   #endif

#endif/*End #ifndef usr_chrg_h*/
